package com.smq.chatserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smq.chatserver.bean.Result;
import com.smq.chatserver.bean.User;
import com.smq.chatserver.bean.UserToken;
import com.smq.chatserver.services.UserService;
import com.smq.chatserver.util.JwtTokenManager;
import com.smq.chatserver.util.SendSms;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

import static com.smq.chatserver.bean.ResultCode.PARAM_INVALID;
import static com.smq.chatserver.bean.ResultCode.SYSTEM_ERROR;

@Api(tags = "登录模块")
@RestController
@RequestMapping("/public")
public class LoginController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "验证手机号是否存在")
    @ApiImplicitParam(name = "mobile", value = "手机号", required = true)
    @PostMapping("/mobileExist")
    public Result<Boolean> mobileExist(String mobile) {
        return Result.success(userService.mobileExist(mobile));
    }

    @ApiOperation(value = "发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "登录/注册", required = true),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true)
    })
    @PostMapping("/mobileCode")
    public Result<Boolean> mobileCode(String key, String mobile) {
        String code = "";
        try {
            code = SendSms.sendSms(mobile);
        } catch (Exception e) {
            return Result.fail(SYSTEM_ERROR);
        }
        System.out.println("mobileCode -> key = " + key + ", mobile = " + mobile + ", sendSms = " + code);
        return Result.success();
    }

    @ApiOperation(value = "验证验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "登录/注册", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", required = true),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true)
    })
    @PostMapping("/checkCode")
    public Result<Boolean> checkCode(String key, String code, String mobile) {
        System.out.println("UserController.checkCode -> key = " + key + ", code = " + code + ", mobile = " + mobile);
        return Result.with(Objects.equals(SendSms.getCode(mobile), code));
    }

    @ApiOperation(value = "注册帐号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = true),
            @ApiImplicitParam(name = "portraitUri", value = "头像地址", required = true),
            @ApiImplicitParam(name = "deviceId", value = "设备标识", required = true),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", required = true),
            @ApiImplicitParam(name = "deviceInfo", value = "登录设备信息", required = true)
    })
    @PostMapping("/reg")
    public Result<UserToken> reg(String name, String portraitUri, String deviceId, String mobile, String code, String deviceInfo) {
        userService.save(new User(null, name, mobile, portraitUri, System.currentTimeMillis(), name.toLowerCase(), null));
        return login(deviceId, mobile, code, deviceInfo);
    }

    @ApiOperation(value = "登录帐号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备标识", required = true),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", required = true),
            @ApiImplicitParam(name = "deviceInfo", value = "登录设备信息", required = true)
    })
    @PostMapping("/login")
    public Result<UserToken> login(String deviceId, String mobile, String code, String deviceInfo) {
        System.out.println("device = " + deviceId + ", mobile = " + mobile + ", code = " + code + ", deviceInfo = " + deviceInfo);
        User user = userService.getOne(new QueryWrapper<User>().eq("mobile", mobile));
        if (user == null) {
            return Result.fail(PARAM_INVALID, mobile);
        } else {
            String token = JwtTokenManager.createToken(user, deviceId, deviceInfo);
            return Result.success(new UserToken(mobile + deviceId, Objects.requireNonNull(user.getUserId()).toString(), deviceId, token, deviceInfo));
        }
    }
}
